package org.ztr.yanai.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.ztr.yanai.blog.dto.CreatePostDTO;
import org.ztr.yanai.blog.dto.PageRequestDTO;
import org.ztr.yanai.blog.dto.PostDTO;
import org.ztr.yanai.blog.dto.UpdatePostDTO;
import org.ztr.yanai.blog.entity.Post;
import org.ztr.yanai.blog.enums.PostStatusEnum;
import org.ztr.yanai.blog.service.PostService;
import org.ztr.yanai.blog.utils.EntityDtoConverter;
import org.ztr.yanai.blog.utils.Result;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/11/22 10:00
 * @Description: 文章管理控制器，提供文章CRUD操作
 * @Version: 1.0
 */
@RestController
@RequestMapping("/post")
@Tag(name = "文章管理", description = "文章相关的 API 接口")
@Validated
public class PostController {

    private final PostService postService;

    public PostController(PostService postService) {
        this.postService = postService;
    }

    @PostMapping
    @Operation(summary = "创建文章", description = "根据传入的文章信息创建新文章")
    public Result<PostDTO> createPost(@Valid @RequestBody CreatePostDTO createPostDTO) {
        Post post = postService.createPost(createPostDTO);
        return Result.success(EntityDtoConverter.toPostDTO(post));
    }

    @DeleteMapping("/{postId}")
    @Operation(summary = "删除文章", description = "根据文章 ID 删除文章（逻辑删除）")
    @Parameter(name = "postId", description = "文章 ID", required = true)
    public Result<Void> deletePost(@PathVariable Long postId) {
        postService.deletePostById(postId);
        return Result.success();
    }

    @GetMapping("/{postId}")
    @Operation(summary = "获取文章详情", description = "根据文章 ID 获取文章详细信息，并增加浏览量")
    @Parameter(name = "postId", description = "文章 ID", required = true)
    public Result<PostDTO> getPostById(@PathVariable Long postId) {
        Post post = postService.getPostByIdWithAuthor(postId);
        return Result.success(EntityDtoConverter.toPostDTO(post));
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询文章", description = "支持分页和关键字查询文章列表")
    public Result<Page<PostDTO>> listPostsByPage(
            @Valid @ModelAttribute PageRequestDTO pageRequest,
            @RequestParam(required = false) PostStatusEnum status) {
        
        Page<Post> postPage = postService.listPostsByPage(pageRequest, status);
        
        // 转换为DTO
        Page<PostDTO> dtoPage = new Page<>(postPage.getCurrent(), postPage.getSize(), postPage.getTotal());
        List<PostDTO> dtoList = postPage.getRecords().stream()
                .map(EntityDtoConverter::toPostDTO)
                .collect(Collectors.toList());
        dtoPage.setRecords(dtoList);
        
        return Result.success(dtoPage);
    }

    @PutMapping
    @Operation(summary = "更新文章", description = "根据传入的文章信息更新文章数据")
    public Result<PostDTO> updatePost(@Valid @RequestBody UpdatePostDTO updatePostDTO) {
        Post post = postService.updatePostInfo(updatePostDTO);
        return Result.success(EntityDtoConverter.toPostDTO(post));
    }

    @PostMapping("/batch/status")
    @Operation(summary = "批量更新文章状态", description = "根据文章 ID 列表批量更新文章状态")
    public Result<Void> batchUpdateStatus(
            @RequestBody List<Long> postIds,
            @RequestParam PostStatusEnum status) {
        postService.batchUpdatePostStatus(postIds, status);
        return Result.success();
    }
}